#define _CRT_SECURE_NO_WARNINGS 1

#include"Heap.h"

void HPInit(HP* php)
{
	assert(php);
	php->a = NULL;
	php->capacity = php->size = 0;
}

void HPDestroy(HP* php)
{
	assert(php);
	free(php->a);
	php->a = NULL;
	php->capacity = php->size = 0;
}

void AdjustUp(HPDataType* a, int child)
{
	int father = (child - 1) / 2;
	while (child > 0)
	{
		if (a[child] < a[father])
		{
			//Swap(&a[child], &a[father]);
			child = father;
			father = (child - 1) / 2;
		}
	}
}


void HPPush(HP* php, HPDataType x)
{
	assert(php);
	if (php->size == php->capacity)
	{
		int newcapacity = php->capacity == 0 ? 4 : php->capacity * 2;
		HPDataType* tmp = (HPDataType*)realloc(php->a, sizeof(HPDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		php->a = tmp;
		php->capacity = newcapacity;
	}
	php->a[php->size++] = x;
	AdjustUp(php->a, php->size - 1);
}

void HPPop(HP* php);

HPDataType HPTop(HP* php);

bool HPEmpty(HP* php);

int HPSize(HP* php);
